package com.boot.main.controller;

import com.boot.main.entity.ScomOrder;
import com.boot.main.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/v1/api")
@Api(tags = "订单管理")
public class OrderController {
    @Resource
    private IOrderService orderService;

    @RequestMapping(value="/orders", method = RequestMethod.GET)
    @ApiOperation(value="获取订单列表")
    public Object getOrders(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, @RequestParam("status") Integer status){
        return orderService.getOrders(page, limit, status);
    }

    @RequestMapping(value="/shop/orders", method = RequestMethod.GET)
    @ApiOperation(value="获取订单列表")
    public Object getShopOrders(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, @RequestParam("status") Integer status){
        return orderService.getShopOrders(page, limit, status);
    }

    @RequestMapping(value="/order", method = RequestMethod.POST)
    @ApiOperation(value="新增订单")
    public Object saveOrder(@RequestBody ScomOrder order){
        return orderService.saveOrder(order);
    }

    @RequestMapping(value="/order/{id}", method = RequestMethod.PUT)
    @ApiOperation(value="订单退款")
    public Object refundOrder(@RequestBody Map map, @PathVariable("id") Long id){
        Integer refund_type = Integer.parseInt(map.get("refund_type").toString());
        String refund_remark = map.get("refund_remark").toString();
        Long refund_time = (new Date()).getTime();
        return orderService.refundOrder(refund_type,refund_remark, refund_time, id);
    }

    @RequestMapping(value="/order/{id}/{status}", method = RequestMethod.PUT)
    @ApiOperation(value="修改订单状态")
    public Object changeOrderStatus(@PathVariable("status") Integer status, @PathVariable("id") Long id){
        return orderService.changeOrderStatus(status, id);
    }

    @RequestMapping(value="/order/{id}/postno/{post_no}", method = RequestMethod.PUT)
    @ApiOperation(value="发货")
    public Object changeOrderPostno(@PathVariable("post_no") String post_no, @PathVariable("id") Long id){
        return orderService.changeOrderPostno(post_no, id);
    }

    @RequestMapping(value="/order/{id}/{del}", method = RequestMethod.DELETE)
    @ApiOperation(value="删除订单")
    public Object deleteOrder(@PathVariable Long id, @PathVariable Integer del){
        return orderService.deleteOrder(id, del);
    }
}
